<?php

namespace App\Admin\Controllers;

use App\Models\CaseEvent;
use App\Models\CaseContact;
use App\Models\CaseCustomer;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Admin;
use App\Models\AdminUser;
use Dcat\Admin\Http\Controllers\AdminController;

class CaseEventController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        if (!Admin::user()->isRole('administrator')) {
            $event = CaseEvent::whereHas('caseCustomer', function ($query) {
                $query->where('admin_user_id', Admin::user()->id);
            });
        } else {
            $event = new CaseEvent();
        }

        return Grid::make($event, function (Grid $grid) {
            $grid->id->sortable();
            $grid->content->width('30%');


            $grid->admin_user_id('跟进人员')->display(function ($id) {
                return optional(AdminUser::find($id))->name;
            });


            $grid->case_customer_id('所属客户')->display(function ($id) {
                return optional(CaseCustomer::find($id))->name;
            })->link(function () {
                return admin_url('customers/' . $this->case_customer_id);
            });

            $grid->case_contact_id('联系人')->display(function ($id) {
                return optional(CaseContact::find($id))->name;
            })->link(function () {
                return admin_url('contacts/' . $this->case_contact_id);
            });

            $grid->created_at('跟进时间')->sortable();
            $grid->disableBatchActions();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableViewButton();
            $grid->disableRefreshButton();
            $grid->toolsWithOutline(false);
            $grid->disableFilterButton();
            $grid->disableActions();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            $grid->model()->orderBy('id', 'desc');
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $detalling = Admin::user()->id != CaseCustomer::find(CaseEvent::find($id)->caseCustomer->id)->adminUser->id;;
        $Role = !Admin::user()->isRole('administrator');
        if ($Role && $detalling) {
            $customer = CaseCustomer::find($id);
            $this->authorize('update', $customer);
        }
        return Show::make($id, new CaseEvent(), function (Show $show) {
            $show->id;
            $show->content;
            $show->case_customer_id;
            $show->case_contact_id;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(CaseEvent::with(['caseContact','adminUser']), function (Form $form) {
            $Editing = $form->isEditing() && Admin::user()->id != CaseCustomer::find($form->model()->case_customer_id)->admin_user_id;
            if ($Editing) {
                $customer = CaseCustomer::find($form->model()->id);
                $this->authorize('update', $customer);
            }
            $form->display('id');
            $form->text('content');
            $form->text('case_customer_id')->type('number');
            $form->text('case_contact_id')->type('number');
            $form->text('case_contract_id')->type('number');
            $form->text('case_opportunity_id')->type('number');
            $form->text('case_batch_id')->type('number');
            $form->text('case_case_id')->type('number');
            $form->text('admin_user_id')->type('number');
            $form->display('created_at');
            $form->display('updated_at');

            $form->saved(function (Form $form) {
                if ($form->case_opportunity_id) {
                    return $form->response()->success('发布成功')->redirect('caseopportunitys/' . $form->case_opportunity_id);
                } elseif ($form->case_contract_id) {
                    return $form->response()->success('发布成功')->redirect('casecontracts/' . $form->case_contract_id);
                } elseif ($form->case_case_id) {
                    return $form->response()->success('发布成功')->redirect('casecases/' . $form->case_case_id);
                } else {
                    return $form->response()->success('发布成功')->redirect('casecustomers/' . $form->case_customer_id);
                }

            });

            $form->deleted(function (Form $form, $result) {
                // 通过 $result 可以判断数据是否删除成功
                return $form->response()->success('删除成功')->redirect('casecustomers/' . $form->case_customer_id);
            });
        });
    }
}
